package kr.hkit.androidcomplete03.ch27;

import kr.hkit.androidcomplete03.R;
import android.app.Activity;
import android.content.ClipData;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

public class DragCoin extends Activity {

	Button btnSource;
	LinearLayout uplinear, downlinear;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dragcoin);
		
		findViewById(R.id.coin500).setOnTouchListener(mTouchListener);
		findViewById(R.id.coin100).setOnTouchListener(mTouchListener);
		findViewById(R.id.coin50).setOnTouchListener(mTouchListener);
		
		findViewById(R.id.uplinear).setOnDragListener(mDragListener);
		findViewById(R.id.downlinear).setOnDragListener(mDragListener);
	}
	
	// 그림을 눌럿을 때 이벤트
	View.OnTouchListener mTouchListener = new View.OnTouchListener() {
		@Override
		public boolean onTouch(View v, MotionEvent event) {
			
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				ClipData clip = ClipData.newPlainText("", "");
				v.startDrag(clip, new CanvasShadow(v, (int)event.getX(), (int)event.getY()), v, 0);
				v.setVisibility(View.INVISIBLE);
				return true;
			}
			return false;
		}
	};
	
	// 사용자가 정의한 클래스
	class CanvasShadow extends View.DragShadowBuilder{
		int mWidth, mHeight;
		int mX, mY;
		
		public CanvasShadow(View v, int x, int y){
			super(v);
			
			mWidth = v.getWidth();
			mHeight = v.getHeight();
			mX = x;
			mY = y;
		}
		
		public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint){
			shadowSize.set(mWidth, mHeight);
			shadowTouchPoint.set(mX, mY);
		}
		
		public void onDrawShadow(Canvas canvas){
			super.onDrawShadow(canvas);
		}
	}
	
	// uplinear, downlinear로 이동 시 기존의 View를 지우고 새로 그려주는 것.
	View.OnDragListener mDragListener = new View.OnDragListener() {
		
		@Override
		public boolean onDrag(View v, DragEvent event) {
			
			switch(event.getAction()){
			
			case DragEvent.ACTION_DRAG_STARTED:
				return true;
				
			case DragEvent.ACTION_DRAG_ENTERED:
				return true;
				
			case DragEvent.ACTION_DRAG_EXITED:
				return true;
			
			// Drop 했을 떄 발생하는 이벤트	
			case DragEvent.ACTION_DROP:
				View view = (View) event.getLocalState();	// View 정보를 불러오는 것 : ( getLocalState() )
				ViewGroup parent = (ViewGroup) view.getParent();
				parent.removeView(view);					// Linearview에서 childView 부분을 지워줌.
				LinearLayout newparent = (LinearLayout)v;	// 사용자가 이동시킨 LinearLayout 부분에 생성
				newparent.addView(view);
				view.setVisibility(View.VISIBLE);
				return true;
			
			// 드래그가 끝났을 때
			case DragEvent.ACTION_DRAG_ENDED:
				if(event.getResult() == false){
					((View)event.getLocalState()).setVisibility(View.VISIBLE);
				}
				return true;
			}
			return true;
		}
	};
	
}
